using System;
using AntEater.ComponentModel.Validation;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Globalization;

namespace AntEater.ComponentModel.BuildElements
{
    /// <summary>
    /// Provides a base implementation for build elements with dynamic names.
    /// </summary>
    [Serializable]
    public abstract class GenericBuildElementBase : BuildElementBase, IGenericBuildElement
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:GenericBuildElementBase"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        protected GenericBuildElementBase(BuildFileDefinition context)
            : base(context) {

            this._elementName = "undefined";
        }

        private string _elementName;

        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <value>The name of the element.</value>
        [BuildElementProperty("ElementName", true, BuildElementPropertyType.String, IsDynamicElementName = true)]
        [Description("Required attribute.\nThe Name of the Element.")]
        [DisplayName("Name")]
        [Category("MsBuild")]
        public virtual string ElementName {
            get { return _elementName; }
            set {
                if (_elementName != value) {
                    _elementName = value;
                    OnPropertyChanged("ElementName");
                }
            }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public abstract override bool IsValid {
            get;
        }

        /// <summary>
        /// Validates the specified recursive.
        /// </summary>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns></returns>
        public abstract override ReadOnlyCollection<ValidationResult> Validate(bool recursive);

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString() {
            return string.Format(CultureInfo.CurrentCulture,"{0} ({1})", base.ToString(), this.ElementName);
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public override object Clone() {
            GenericBuildElementBase newElement = base.Clone() as GenericBuildElementBase;

            if (this._elementName != null) {
                newElement._elementName = this._elementName.Clone() as string;
            }

            return newElement;
        }
    }
}
